package com.supreme.threadserver;

import com.supreme.commonnetty.server.NettyEchoServer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@SpringBootApplication
public class SupremeThreadServerApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(SupremeThreadServerApplication.class, args);
        System.out.println(" _____ _   _ ____  _____    _    ____    ____  _____ ______     _______ ____\n" +
                "|_   _| | | |  _ \\| ____|  / \\  |  _ \\  / ___|| ____|  _ \\ \\   / / ____|  _ \\\n" +
                "  | | | |_| | |_) |  _|   / _ \\ | | | | \\___ \\|  _| | |_) \\ \\ / /|  _| | |_) |\n" +
                "  | | |  _  |  _ <| |___ / ___ \\| |_| |  ___) | |___|  _ < \\ V / | |___|  _ <\n" +
                "  |_| |_| |_|_| \\_\\_____/_/   \\_\\____/  |____/|_____|_| \\_\\ \\_/  |_____|_| \\_\\");
        System.out.println(" _______________\n" +
                "< THREAD SERVER >\n" +
                " ---------------\n" +
                "          \\\n" +
                "           \\\n" +
                "            \\          __---__\n" +
                "                    _-       /--______\n" +
                "               __--( /     \\ )XXXXXXXXXXX\\v.\n" +
                "             .-XXX(   O   O  )XXXXXXXXXXXXXXX-\n" +
                "            /XXX(       U     )        XXXXXXX\\\n" +
                "          /XXXXX(              )--_  XXXXXXXXXXX\\\n" +
                "         /XXXXX/ (      O     )   XXXXXX   \\XXXXX\\\n" +
                "         XXXXX/   /            XXXXXX   \\__ \\XXXXX\n" +
                "         XXXXXX__/          XXXXXX         \\__---->\n" +
                " ---___  XXX__/          XXXXXX      \\__         /\n" +
                "   \\-  --__/   ___/\\  XXXXXX            /  ___--/=\n" +
                "    \\-\\    ___/    XXXXXX              '--- XXXXXX\n" +
                "       \\-\\/XXX\\ XXXXXX                      /XXXXX\n" +
                "         \\XXXXXXXXX   \\                    /XXXXX/\n" +
                "          \\XXXXXX      >                 _/XXXXX/\n" +
                "            \\XXXXX--__/              __-- XXXX/\n" +
                "             -XXXXXXXX---------------  XXXXXX-\n" +
                "                \\XXXXXXXXXXXXXXXXXXXXXXXXXX/\n" +
                "                  \"\"VXXXXXXXXXXXXXXXXXXV\"\"");
    }

    @Value("${netty.server.port}")
    private Integer nettyServerPort;

    @Resource
    private NettyEchoServer nettyEchoServer;

    @Resource
    private ThreadPoolTaskExecutor taskExecutor;

    @Override
    public void run(String... args) throws Exception {
        nettyEchoServer.startServer(nettyServerPort);

        taskExecutor.execute(() -> {
            try {
                TimeUnit.SECONDS.sleep(5);
                System.out.println("<=== <=== <=== taskExecutor running");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }
}
